<?php

namespace app\model;

use think\Model;

class ProductOrder extends Model
{
    const PAY_REFUNDING = -3;
    const PAY_REFUND = -1;
    const PAY_FAIL = 0;
    const PAY_SUCCESS = 1;
    const PAY_All_SUCCESS = 2;

    public static function getOrderInfo($order_id)
    {
        $order = ProductOrder::where('order_id', $order_id)->find();
        if($order){
            $order = $order->toArray();
            $product = Db('product')->where('product_id', $order['product_id'])->find();
            $order['product_deposit'] = $product['deposit'];
            $order['product_money'] = $product['money'];
            $order['product_description'] = $product['description'];
            if ($order['status'] != 2 && ((time() - strtotime($order['create_time'])) > $product['validity_day']*24*60*60) ) {
                $order['status'] = -2;
            }
        }else{
            $order = [];
        }
        return $order;
    }

    public static function getTransaction()
    {
        do {
            $order = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . mt_rand(10, 99);
        } while (ProductOrder::where('transaction', $order)->find());
        return $order;
    }

    public static function setStatus($order_id, $status)
    {
        $info = ProductOrder::where('order_id', $order_id)->find()->toArray();
        if (ProductOrder::where('order_id', $order_id)->setField('status', $status) !== false) {
            if ($status == self::PAY_SUCCESS && $info['status'] != self::PAY_SUCCESS) {
                ProductOrder::where('order_id', $order_id)->setField('pay_time', date('Y-m-d H:i:s'));
                ProductOrder::where('order_id', $order_id)->setField('all_pay_time', null);
            }
            if ($status == self::PAY_REFUND && $info['status'] == self::PAY_REFUNDING) {
                ProductOrder::where('order_id', $order_id)->setField('refund_time', date('Y-m-d H:i:s'));
            }
            if ($status == self::PAY_All_SUCCESS && $info['status'] != self::PAY_All_SUCCESS) {
                ProductOrder::where('order_id', $order_id)->setField('all_pay_time', date('Y-m-d H:i:s'));
                ProductOrder::where('order_id', $order_id)->setField('price', $info['product_price']);
                $product = Db('product')->where('product_id',$info['product_id'])->find();
                if($product['medal_id']){
                    if(!Db('userMedal')->where('user_id',$info['user_id'])->where('medal_id',$product['medal_id'])->find()){
                        $medal = Db('medal')->where('medal_id',$product['medal_id'])->find();
                        $data = [
                            'user_id' => $info['user_id'],
                            'medal_id' => $medal['medal_id'],
                            'medal_path' => $medal['image'],
                            'create_time' => date('Y-m-d H:i:s'),
                        ];
                        Db('userMedal')->insert($data);
                    }
                }
            }
            return true;
        }
        return false;
    }
}
